﻿using System;
using System.Data;
using System.Drawing;
using System.IO;
using Website.Data;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Net;

namespace Website
{
    public partial class ImagePreview : System.Web.UI.Page
    {
        const string K_ImageDirectory = "/";
        protected void Page_Load(object sender, EventArgs e)
        {
            Int32 IdType = Int32.MinValue;
            string _Filename = string.Empty;
            string _FolderName = string.Empty;
            string _Mode = string.Empty;

            if (Request.QueryString["Type"] != null)
                IdType = Convert.ToInt32(Request.QueryString["Type"].ToString());

            if (Request.QueryString["mode"] != null)
                _Mode = Request.QueryString["mode"].ToString();

            if (Request.QueryString["dn"] != null)
                _FolderName = Request.QueryString["dn"].ToString();

            if (Request.QueryString["fn"] != null)
                _Filename = Request.QueryString["fn"].ToString();


            CreateResponse(_Mode,IdType,_FolderName,_Filename);
        }

        protected byte[] LoadData(string mode, int type, string foldername, string filename)
        {
            try
            {
                //http://www.dev-poleposition.com/Dev/Images/Vendor_Visit_XMissionStructures/1500-1599/1502_2202.jpg
                string vendor = "";

                switch (type)
                {
                    case 1:
                        vendor = "GPS";
                        break;
                    case 2:
                        vendor = "Inspection";
                        break;
                    case 3:
                        vendor = "DistributionGPS";
                        break;
                    case 4:
                        vendor = "DistributionGPSMeterImages";
                        break;
                    case 5:
                        vendor = "DistributionInspection";
                        break;
                    case 6:
                        vendor = "DistributionInspectionMeterImages";
                        break;
                }

                /*
                if (type == 1)
                    vendor = "GPS";
                else
                    vendor = "Inspection";
                */


                string url = "http://www.dev-poleposition.com/" + mode + "/Images/" + vendor + "/" + foldername + "/" +
                             filename + ".jpg";

                WebClient _Brownser = new WebClient();
                byte[] Buffer = _Brownser.DownloadData(url);
                return Buffer;
            }
            catch (Exception ex)
            {
                throw ex;

            }
        }

        protected void CreateResponse(string mode, int type, string foldername, string filename)
        {
            byte[] _image = LoadData(mode,type,foldername,filename);
            int _width = 0;
            if (Request.QueryString["w"] != null)
                _width = Convert.ToInt32(Request.QueryString["w"].ToString());
            int _heigth = 0;
            if (Request.QueryString["w"] != null)
                _heigth = Convert.ToInt32(Request.QueryString["h"].ToString());


            if (_heigth != 0 && _width != 0)
            {
                _image = ResizeImageFile(_image, _width, _heigth);
            }
            SendResponse(_image);
        }

        protected void SendResponse(byte[] Buffer)
        {
            if (Buffer.Length > 0)
            {
                MemoryStream ms = null;
                ms = new MemoryStream(Buffer, 0, Buffer.Length);
                Response.Clear();
                Response.ContentType = "image/jpeg";
                ms.WriteTo(Response.OutputStream);
                ms.Close();
            }

        }

        public byte[] ResizeImageFile(byte[] imageFile, int width, int heigth)
        {
            Image original = null;
            Image imgPhoto = null;
            Bitmap bmPhoto = null;
            Graphics grPhoto = null;
            MemoryStream mm = null;
            try
            {
                original = Image.FromStream(new MemoryStream(imageFile));
                int targetH, targetW;
                if (original.Height > original.Width)
                {
                    targetH = heigth;
                    targetW = (int)(original.Width * ((float)heigth / (float)original.Height));
                }
                else
                {
                    targetW = width;
                    targetH = (int)(original.Height * ((float)heigth / (float)original.Width));
                }
                imgPhoto = Image.FromStream(new MemoryStream(imageFile));
                // Create a new blank canvas.  The resized image will be drawn on this canvas.
                bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
                grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
                // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
                mm = new MemoryStream();
                bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
                return mm.GetBuffer();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                original.Dispose();
                imgPhoto.Dispose();
                bmPhoto.Dispose();
                grPhoto.Dispose();
                mm.Close();
                mm.Dispose();
            }
        }
    }
}
